package com.white.algebra.service.impl;

import com.white.algebra.dao.PermissionDao;
import com.white.algebra.dao.RoleDao;
import com.white.algebra.dao.UserDao;
import com.white.algebra.dao.impl.PermissionDaoImpl;
import com.white.algebra.dao.impl.RoleDaoImpl;
import com.white.algebra.dao.impl.UserDaoImpl;
import com.white.algebra.domain.Permission;
import com.white.algebra.domain.Role;
import com.white.algebra.domain.UserInfo;
import com.white.algebra.service.UserService;
import com.white.algebra.utils.PasswordEncoderUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDao userDao=new UserDaoImpl();;

    @Override
    public UserInfo login(String username, String password) throws Exception {
        UserInfo userInfo=userDao.login(username,password);
        return userInfo;
    }

    @Override
    public void register(String username, String password,String email) throws SQLException {
        userDao.register(username,password,email);
    }

    @Override
    public List<UserInfo> findAll(int startRecord, int size, String fuzzyName) throws SQLException {
        return userDao.findAll(startRecord,size,fuzzyName);
    }

    @Override
    public UserInfo findById(String id) throws Exception {
        UserInfo userInfo= userDao.findById(id);
        RoleDao roleDao=new RoleDaoImpl();
        userInfo.setRoles(roleDao.findRoleByUserId(userInfo.getId()));
        PermissionDao permissionDao=new PermissionDaoImpl();
        for (Role r:userInfo.getRoles()
             ) {
            r.setPermissions(permissionDao.findById(r.getId()));
        }
        return userInfo;
    }

    @Override
    public void save(UserInfo userInfo) throws SQLException {
        userInfo.setPassword(PasswordEncoderUtils.getMd5(userInfo.getPassword()));
        userDao.save(userInfo);
    }

    @Override
    public void relatedRole(String userId, String roleId) throws SQLException {
        userDao.relatedRole(userId,roleId);
    }

    @Override
    public void deleteRoleById(String id, String userId) throws SQLException {
        userDao.deleteRoleById(id,userId);
    }

    @Override
    public int getLogNum(String fuzzyName) throws SQLException {
        return userDao.getLogNum(fuzzyName);
    }

    @Override
    public List<Role> findNotRoleByUserId(String id) throws SQLException {
        RoleDao roleDao=new RoleDaoImpl();
        return roleDao.findNotRoleByUserId(id);
    }

    @Override
    public ArrayList<String> getAllPermission(String id) throws Exception {
        RoleDao roleDao=new RoleDaoImpl();
        PermissionDao permissionDao=new PermissionDaoImpl();
        ArrayList<String> arrayList=new ArrayList<>();
        List<Role> roles=roleDao.findRoleByUserId(id);
        for (Role r:roles) {
            List<Permission> permission = permissionDao.findById(r.getId());
            for (Permission p : permission) {
                arrayList.add(p.getUrl());
            }
        }
        return arrayList;
    }
}
